package com.fadadianqi.mall.filter;

import com.fadadianqi.mall.web.controller.MemberHelper;
import com.yeqiangwei.commons.util.HttpSessionUtils;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component("filter_mallWebFilter")
public class MallFilter implements Filter {

    protected FilterConfig filterConfig = null;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
                         FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        String URI = request.getRequestURI();

		/*
		 * 登录时是否显示验证码
		 */
        Boolean showVcode = HttpSessionUtils.getBoolean(request, "showVcode", Boolean.FALSE);
        request.setAttribute("showVcode", showVcode);

        request.setAttribute("sessionMember", MemberHelper.getMember(request));

        filterChain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        filterConfig = null;
    }
}
